package com.zbbt.sportslottery.ui.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.BallDataBean;
import com.zbbt.sportslottery.bean.DrawNotice;
import com.zbbt.sportslottery.bean.SuperResult;
import com.zbbt.sportslottery.db.DbManager;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.adapter.DrawNoticeAdapter;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout.OnRefreshListener;

/**
 * 
 * @ClassName: DrawNoticeActivity
 * @Description: 开奖公告
 * @author：GaomingShuo
 * @date：2015-4-8 下午2:28:47
 * @version V1.0
 */
public class DrawNoticeActivity extends SimpleBaseActivity {

	private SwipeRefreshLayout refreshLayout;
	private ListView noticeListView;
	private DrawNoticeAdapter adapter;
	private List<BallDataBean> responseAwardList = new ArrayList<BallDataBean>();
	private ProgressBarCircularIndetermininate progressBar;

	public DrawNoticeActivity() {}

	public static DrawNoticeActivity getInstance() {
		return new DrawNoticeActivity();
	}

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.activity_award_notice);
	}

	@Override
	protected void initView() {
		progressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.progressBarCircularIndetermininate);
		refreshLayout = (SwipeRefreshLayout) findViewById(R.id.notice_swipe_container);
		noticeListView = (ListView) findViewById(R.id.notice_listview);
		adapter = new DrawNoticeAdapter(context, responseAwardList);
		noticeListView.setAdapter(adapter);
		noticeListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				int itemViewType = adapter.getItemViewType(position);
				BallDataBean entity = responseAwardList.get(position);
				if (itemViewType != 4) {// adapter里的bottom类型
					Intent i = new Intent(context, DrawHistoryActivity.class);
					i.putExtra("type", entity.getLotteryType() + "");
					i.putExtra("name", entity.getLotteryName());
					i.putExtra("lotterycode", entity.getLotteryCode() + "");
					i.putExtra("areaId", entity.getAppId() + "");
					context.startActivity(i);
				}
			}
		});
		refreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				initData();
			}
		});

		progressBar.setVisibility(View.VISIBLE);
		initData();
	}

	@Override
	protected void setListener() {

	}

	@Override
	protected void onClickEvent(View v) {

	}

	@Override
	protected void initData() {
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("lotteryType", App.PARAM_CATEGORY);
		jsonObject.addProperty("productType", 2);
		ZbHttpClient.postNewApi(ServerConfig.API_DRAW_NOTICE_OID, jsonObject, new TypeGsonHttpHandler<SuperResult<BallDataBean>>(true) {

			@Override
			public void onSuccess(SuperResult<BallDataBean> data) {
				if (data != null) {
					responseAwardList.clear();
					responseAwardList.addAll(data.getData());
					adapter.notifyDataSetChanged();
					refreshLayout.setRefreshing(false);
					progressBar.setVisibility(View.GONE);
					// 数据备份
					DbManager.saveAwardlist(data.getData());
				}
			}

			@Override
			public void onFailure(String errorCode, String errorMsg) {
				responseAwardList.clear();
				responseAwardList.addAll(DbManager.getAwardList());
				adapter.notifyDataSetChanged();
				refreshLayout.setRefreshing(false);
				progressBar.setVisibility(View.GONE);
			}

		});

	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

}
